package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.userInfo.entity.City;

import java.util.List;

public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    public List<City.ResponseBean> responseBeans = null;

    //interface
    private OnItemClickListener mOnItemClickListener = null;

    public CityAdapter(Context context) {
        this.mContext = context;
    }

    public void setResponseBeans(List<City.ResponseBean> list) {
        this.responseBeans = list;
    }

    public List<City.ResponseBean> getResponseBeans() {
        return this.responseBeans;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_city_item_view, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        City.ResponseBean responseBean = responseBeans.get(position);
        CityViewHolder cityViewHolder = (CityViewHolder) holder;
        cityViewHolder.txt_city.setText(responseBean.getTitle());
        cityViewHolder.layout_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return responseBeans == null ? 0 : responseBeans.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class CityViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout layout_city;

        private TextView txt_city;

        public CityViewHolder(View itemView) {
            super(itemView);
            txt_city = itemView.findViewById(R.id.txt_city);
            layout_city = itemView.findViewById(R.id.layout_city);
        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {

        void onItemClick(int position);
    }
}
